'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.errorHandlerScript = exports.strArgs = undefined;

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

var _safeStringify = require('./safeStringify');

var _safeStringify2 = _interopRequireDefault(_safeStringify);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var strArgs = exports.strArgs = function strArgs(args, mem) {
    var rst = [];
    var arg;
    if (!mem) {
        mem = [];

        mem._atotal = 9999;
    } else {
        if (mem._atotal-- <= 0) {
            return rst;
        };
    }

    for (var i = 0; i < args.length; i++) {
        arg = args[i];

        if (mem.indexOf(arg) > -1) {
            continue;
        } else {
            mem.push(arg);
        }

        if (arg === window || arg === document) {
            continue;
        }

        if (arg instanceof Array) {
            rst = rst.concat(strArgs(arg));
            continue;
        }
        if (arg instanceof RegExp) {
            rst.push(x);
            continue;
        }
        if (arg instanceof Error) {
            try {
                rst.push(['err stack:', arg.message, arg.stack].join(''));
            } catch (ex) {}
            continue;
        }
        if ((typeof arg === 'undefined' ? 'undefined' : (0, _typeof3.default)(arg)) === 'object') {
            for (var k in arg) {
                rst = rst.concat(['{', k, ':', strArgs([arg[k]], mem).join(','), '}'].join(''));
            }
            continue;
        }
        rst.push(arg);
    }
    return rst;
};

var errorHandlerScript = exports.errorHandlerScript = function errorHandlerScript() {
    (function () {
        function strArgs(args, mem) {
            var rst = [];
            var arg;
            if (!mem) {
                mem = [];

                mem._atotal = 9999;
            } else {
                if (mem._atotal-- <= 0) {
                    return rst;
                };
            }

            for (var i = 0; i < args.length; i++) {
                arg = args[i];

                if (mem.indexOf(arg) > -1) {
                    continue;
                } else {
                    mem.push(arg);
                }

                if (arg === window || arg === document) {
                    continue;
                }

                if (arg instanceof Array) {
                    rst = rst.concat(strArgs(arg));
                    continue;
                }
                if (arg instanceof RegExp) {
                    rst.push(x);
                    continue;
                }
                if (arg instanceof Error) {
                    try {
                        rst.push(['err stack:', arg.message, arg.stack].join(''));
                    } catch (ex) {}
                    continue;
                }
                if ((typeof arg === 'undefined' ? 'undefined' : (0, _typeof3.default)(arg)) === 'object') {
                    for (var k in arg) {
                        rst = rst.concat(['{', k, ':', strArgs([arg[k]], mem).join(','), '}'].join(''));
                    }
                    continue;
                }
                rst.push(arg);
            }
            return rst;
        }
        function alertErr(tag, _arguments, others) {
            tag = tag instanceof Array ? tag : [tag];
            var args = Array.prototype.slice.call(_arguments);
            args = tag.concat(args, others || []);
            var msg = strArgs(args).join('->');
            alert(msg);
            try {
                console.log(msg);
            } catch (ex) {}
        }
        window.onerror = function () {
            alertErr('windowErr', arguments);
        };
        window.onunhandledrejection = function (e) {
            alertErr(['promiseErr', e.reason, e], arguments);
        };
    })();
};